package top.vanchor.myblog2back.controller.publics;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.vanchor.myblog2back.config.JwtConfigProperties;
import top.vanchor.myblog2back.entity.BackAdmin;
import top.vanchor.myblog2back.entity.BackAuthGroup;
import top.vanchor.myblog2back.entity.BackAuthGroupRule;
import top.vanchor.myblog2back.entity.BackAuthRule;
import top.vanchor.myblog2back.service.IBackAdminService;
import top.vanchor.myblog2back.service.IBackAuthGroupRuleService;
import top.vanchor.myblog2back.service.IBackAuthGroupService;
import top.vanchor.myblog2back.service.IBackAuthRuleService;
import top.vanchor.myblog2back.utils.JWTUtils;
import top.vanchor.myblog2back.utils.JwtInterceptor;
import top.vanchor.myblog2back.utils.SMSUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Ding
 * @create 2022/1/27 12:30
 */
@Api("新型冠状病毒大数据")
@RestController
@CrossOrigin
@RequestMapping("/public/sarsCov")
public class SarsCoV {

    /**
     * 获取大数据
     * @return json
     */
    @ApiOperation("获取数据")
    @GetMapping("/getList")
    @ResponseBody
    public JSONObject getList(){
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonTemp = new JSONObject();
        String s = HttpUtil.get("https://c.m.163.com/ug/api/wuhan/app/data/list-total");
        //综合数据，包含请求id等
        JSONObject allBody = JSONObject.parseObject(s);
        //纯数据，所有数据
        JSONObject allData = allBody.getJSONObject("data");
        //地区数据-细节包含（全国）所有省市
        JSONArray allAreaData = allData.getJSONArray("areaTree");
        //全国数据
        JSONObject chinaTotal = allData.getJSONObject("chinaTotal");
        //中国地区数据-所有中国数据
        JSONObject chinaData = new JSONObject();
        for (Object allAreaDatum : allAreaData) {
            JSONObject obj = (JSONObject) allAreaDatum;
            if ("中国".equals(obj.get("name"))) {
                chinaData = obj;
                break;
            }
        }
        //纯中国地区数据，不包含中国总信息
        JSONArray chinaAreaData = chinaData.getJSONArray("children");
        int maxNum = 0;
        for (Object chinaAreaDatum : chinaAreaData) {
            JSONObject obj = (JSONObject) chinaAreaDatum;
            JSONObject obj2 = (JSONObject) obj.get("total");
            int confirm = 0;
            int dead = 0;
            int heal = 0;
            int nowConfirm = 0;
            confirm += Integer.parseInt(String.valueOf(obj2.get("confirm")));
            dead += Integer.parseInt(String.valueOf(obj2.get("dead")));
            heal += Integer.parseInt(String.valueOf(obj2.get("heal")));
            nowConfirm = confirm - dead - heal;
            maxNum = Math.max(nowConfirm, maxNum);
            ((JSONObject) chinaAreaDatum).put("value", nowConfirm);
        }
        jsonObject.put("code",200);
        jsonObject.put("msg","获取成功");
        jsonTemp.put("area",chinaAreaData);
        jsonTemp.put("total",chinaData.get("total"));
        jsonTemp.put("chinaTotal",chinaTotal);
        jsonTemp.put("max",maxNum);
        jsonObject.put("data",jsonTemp);
        return jsonObject;
    }
}
